﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace GameBase
{
    public class MotionEffects : InvisibleGameEntity
    {
        private Vector2 position = Vector2.Zero;
        private Vector2 velocity = Vector2.Zero;
        private Vector2 acceleration = Vector2.Zero;
        private float friction = 0;

        public float Friction
        {
            get { return friction; }
            set { friction = value; }
        }

        public Vector2 Acceleration
        {
            get { return acceleration; }
            set { acceleration = value; }
        }

        public Vector2 Velocity
        {
            get { return velocity; }
            set { velocity = value; }
        }

        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }
        public MotionEffects()
        {
        }
        public MotionEffects(float friction)
        {
            this.friction = friction;
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            
            velocity += acceleration;
            velocity = Move(velocity, Vector2.Zero, friction);
            position += velocity;
        }
        
        public static float Move(float source, float target, float velocity)
        {
            if (Math.Abs(target - source) < velocity)
            {
                return target;
            }
            else if (source < target)
            {
                return source + velocity;
            }
            return source - velocity;
        }
        public static Vector2 Move(Vector2 source, Vector2 target, Vector2 velocity)
        {
            return Move(source, target, velocity.X, velocity.Y);
        }
        public static Vector2 Move(Vector2 source, Vector2 target, float velocity)
        {
            if (target == source)
                return target;
            Vector2 d = target - source;
            if (d.Length() < velocity)
            {
                return target;
            }
            d.Normalize();
            d = d*velocity;
            return source+d;
        }
        public static Vector3 Move(Vector3 source, Vector3 target, float velocity)
        {
            if (target == source)
                return target;
            Vector3 d = target - source;
            if (d.Length() < velocity)
            {
                return target;
            }
            d.Normalize();
            d = d * velocity;
            return source+d;
        }
        public static Vector2 Move(Vector2 source, Vector2 target, float velocityX, float velocityY)
        {
            source.X = Move(source.X, target.X, velocityX);
            source.Y = Move(source.Y, target.Y, velocityY);
            return source;
        }
    }
}
